<style lang="less">
    @import './login.less';
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                    <p class="login-tip" v-show="errorMsg" style="color: #ed3f14;">用户名或者密码错误</p>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie';
import axios from 'axios';
import {baseHost} from '@/libs/config.js';
export default {
    data () {
        return {
            form: {
                userName: '',
                password: ''
            },
            rules: {
                userName: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ]
            },
            errorMsg: false
        };
    },
    methods: {
        handleSubmit () {
            let url = `${baseHost}/oauth/token`;
            let formData = {
                grant_type: 'password',
                client_id: '1',
                client_secret: '61fkplKByLaHxB9PzSJ9ypyNyuFFSP22s8zAQkum',
                username: this.form.userName,
                password: this.form.password,
                scope: ''
            };
            axios.post(url, formData).then(res => {
                if (res.status === 200) {
                    Cookies.set('user', formData.username, { expires: 14 });
                    Cookies.set('token', `Bearer ${res.data.access_token}`, { expires: 14 });
                    this.$store.commit('setAvator', 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3448484253,3685836170&fm=27&gp=0.jpg');
                    this.$router.push({
                        name: 'home_index'
                    });
                }
            }).catch(err => {
                this.errorMsg = true;
            }).finally();
        }
    }
};
</script>

<style>

</style>
